﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DocGia.VO;
using System.Data.SqlClient;
namespace BTL
{
    public partial class frmDocGia1 : Form
    {
        DGVO objdg = new DGVO();
        DAL.DGDAL _dg = new DAL.DGDAL();
        public frmDocGia1()
        {
            InitializeComponent();
            comboBox1.Text = "Mã độc giả";
            
            
        }
        private void DocGiabinding()
        {
            box_madg.DataBindings.Clear();
            box_madg.DataBindings.Add("text", dataGridViewDocGia.DataSource, "MaDG");
            box_ngaysinh.DataBindings.Clear();
            box_ngaysinh.DataBindings.Add("text", dataGridViewDocGia.DataSource, "NgaySinh");
            box_diachi.DataBindings.Clear();
            box_diachi.DataBindings.Add("text", dataGridViewDocGia.DataSource, "DiaChi");
            box_tendg.DataBindings.Clear();
            box_tendg.DataBindings.Add("text", dataGridViewDocGia.DataSource, "TenDG");


        }
        private void data()
        {
            objdg = new DGVO();
            objdg.MDG = box_madg.Text;
            objdg.TDG = box_tendg.Text;
            objdg.DC = box_diachi.Text;
            objdg.NS = Convert.ToDateTime(box_ngaysinh.Text);

 
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            data();
            _dg = new DAL.DGDAL();
            _dg._DocGia_them(objdg);
            frmDocGia1_Load(sender, e);
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                _dg._DocGia_xoa(box_madg.Text);
                dataGridViewDocGia.DataSource = _dg;
                frmDocGia1_Load(sender, e);
            }
            catch
            {
                MessageBox.Show("Doc gia chua chiu tra sach","Loi",MessageBoxButtons.OK);
            }
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {

            data();
            _dg._DocGia_sua(objdg);
            dataGridViewDocGia.DataSource = _dg;
            frmDocGia1_Load(sender, e);
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_hienthi_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from DocGia";
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridViewDocGia.DataSource = dt;
            conn.Close(); 
        }

        private void bt_timkiem_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mã độc giả")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from DocGia where maDG like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewDocGia.DataSource = dt;
                conn.Close();
            }
            if (comboBox1.Text == "Tên độc giả")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from DocGia where tenDG like '%" + textBox1.Text.Trim() + "%'";
                cmd.ExecuteNonQuery();
                da.Fill(dt);
                dataGridViewDocGia.DataSource = dt;
                conn.Close();
            }
        }

        private void frmDocGia1_Load(object sender, EventArgs e)
        {
            bt_hienthi_Click(sender, e);
            DocGiabinding();

        }

        private void frmDocGia1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                frmHelp _help = new frmHelp();
                _help.Show();
            }
        }

        private void dataGridViewDocGia_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmHelp _help = new frmHelp();
            _help.Show();
        }
    }
}
